package com.example.pc.myapplication;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.example.pc.myapplication.adapter.HotAdapter;
import com.example.pc.myapplication.pojo.HotData;
import com.example.pc.myapplication.utils.HttpUtils;
import com.google.gson.Gson;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.rv_main)
    RecyclerView rvMain;
    private HotAdapter hotAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        rvMain.setLayoutManager(new LinearLayoutManager(this));
        hotAdapter = new HotAdapter(this);
        rvMain.setAdapter(hotAdapter);

    }
    private Handler h = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    HotData hotData = (HotData) msg.obj;
                    hotAdapter.setHotData(hotData);
                    break;
            }


            return false;
        }
    });

    @Override
    public void onBackPressed() {

        loadData();
    }

    private void loadData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String s1 = HttpUtils.get("http://mobile.ximalaya.com/discovery-firstpage/v2/explore/ts-1536547537960?appid=0&categoryId=-2&code=43_440000_4403&device=android&deviceId=57cf4217-4dcc-364b-9845-40eaa02775bc&includeActivity=true&includeSpecial=true&inreview=false&network=wifi&operator=2&pullToRefresh=true&scale=1&version=6.5.16");
                String s = HttpUtils.get(HttpUtils.URL_RECOMMEND);
                HotData hotData = new Gson().fromJson(s, HotData.class);
                Message message = h.obtainMessage(1, hotData);
                h.sendMessage(message);

            }
        }).start();
    }
}
